const personalPics = [ //个人照片要求
  {
    url: "../../../images/idcard-correct.png",
    title: "标准拍摄",
    type: 1
  },
  {
    url: "../../../images/idcard-defect.png",
    title: "边框缺失",
    type: 2
  },
  {
    url: "../../../images/idcard-light.png",
    title: "闪光强烈",
    type: 2
  },
  {
    url: "../../../images/idcard-vague.png",
    title: "照片模糊",
    type: 2
  }
];
const enterprisePics = [ //企业照片要求
  {
    url: "../../../images/enterprise-correct.png",
    title: "标准拍摄",
    type: 1
  },
  {
    url: "../../../images/enterprise-defect.png",
    title: "边框缺失",
    type: 2
  },
  {
    url: "../../../images/enterprise-light.png",
    title: "闪光强烈",
    type: 2
  },
  {
    url: "../../../images/enterprise-vague.png",
    title: "照片模糊",
    type: 2
  }
];


const {
  validate
} = require("../../../../utils/validate");
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */

  data: {
    pageType: 1,
    step: 1,
    demandPics: [],
    frontImg: "", //正面
    backImg: "", //反面
    info: '',
  },

  onLoad: function (options) {
    this.id = "";
    let _self = this;
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      let frontImg, backImg;
      if (data.type == 1) {
        _self.personal_ID_card_head_img = data.personal_ID_card_head_img;
        _self.personal_ID_card_national_emblem = data.personal_ID_card_national_emblem;
        frontImg = data.personal_ID_card_head_img_link;
        backImg = data.personal_ID_card_national_emblem_link;
      } else {
        _self.company_business_license = data.company_business_license;
        frontImg = data.company_business_license_link;
        backImg = "";
      }
      _self.id = data.id;
      _self.setData({
        info: data,
        frontImg,
        backImg
      })
    })
    const {
      type: pageType = 1
    } = options; //type 1个人认证 2企业认证
    this.setData({
      pageType,
      demandPics: pageType == 1 ? personalPics : enterprisePics
    })
    console.log(this.data.demandPics)
  },


  //上一步返回入口界面
  navEntrance() {
    wx.navigateBack({
      delta: 1
    })
  },

  prevStep() {
    this.setData({
      step: 1
    })
  },

  //下一步
  nextStep(e) {
    const type = this.data.pageType;
    const form = e.detail.value;
    console.log(form);
    if (type == 1) {
      if (validate._isNullOrEmpty(form.personal_name)) {
        app.showToast("请填写真实姓名");
        return;
      }
      if (validate._isNullOrEmpty(form.personal_ID_card)) {
        app.showToast("请填写身份证号");
        return;
      }
      if (!validate._isIdCard(form.personal_ID_card)) {
        app.showToast("身份证号码错误");
        return;
      }
    } else if (type == 2) {
      if (validate._isNullOrEmpty(form.company_name)) {
        app.showToast("请填写公司名称");
        return;
      }
      if (validate._isNullOrEmpty(form.company_representative)) {
        app.showToast("请填写法人代表");
        return;
      }
      if (validate._isNullOrEmpty(form.company_number)) {
        app.showToast("请填写统一社会信用代码");
        return;
      }
      if (!validate._isSocialCreditCode(form.company_number)) {
        app.showToast("统一社会信用代码错误");
        return;
      }
      if (validate._isNullOrEmpty(form.company_address)) {
        app.showToast("请填写公司地址");
        return;
      }
    }
    this.form = form;
    this.setData({
      step: 2
    })
  },

  chooseImage(e) {
    const {
      type //1正面 2背面
    } = e.currentTarget.dataset;
    wx.chooseImage({
      count: 1,
      success: res => {
        const file = res.tempFiles[0].path;
        wx.showLoading({
          mask: true,
          title: "上传中..."
        })
        wx.uploadFile({
          filePath: file,
          name: 'file',
          url: `${app.globalData._url}Common/Common/upload`,
          success: res => {
            wx.hideLoading();
            if (res.statusCode == 200) {
              const soure = JSON.parse(res.data);
              if (soure.code == 1000) {
                const pageType = this.data.pageType;
                if (type == 1) {
                  this.setData({
                    frontImg: soure.data.all_url
                  })
                  if (pageType == 1) {
                    this.personal_ID_card_head_img = soure.data.url;
                  } else {
                    this.company_business_license = soure.data.url;
                  }
                } else if (type == 2) {
                  this.setData({
                    backImg: soure.data.all_url
                  })
                  this.personal_ID_card_national_emblem = soure.data.url;
                }
              } else {
                app.showToast(soure.message);
              }
              console.log(soure);
            } else {
              app.showToast("网络错误,上传失败");
            }
            console.log(res);
          },
          fail: e => {
            wx.hideLoading();
            app.showToast("上传失败");
          }
        })
      },
      fail: e => {
        app.showToast("唤起选择图片失败");
      }
    })
  },



  //提交数据
  submitInfo() {
    const pageType = this.data.pageType;
    let url; //请求地址
    if (pageType == 1) {
      if (validate._isNullOrEmpty(this.personal_ID_card_head_img)) {
        app.showToast("请上传身份证正面");
        return;
      }
      if (validate._isNullOrEmpty(this.personal_ID_card_national_emblem)) {
        app.showToast("请上传身份证背面");
        return;
      }
      Object.assign(this.form, {
        id: this.id,
        personal_ID_card_national_emblem: this.personal_ID_card_national_emblem,
        personal_ID_card_head_img: this.personal_ID_card_head_img
      });
      url = "Partner/Setup/person_authentication"
    } else if (pageType == 2) {
      if (validate._isNullOrEmpty(this.company_business_license)) {
        app.showToast("请上传营业执照");
        return;
      }
      Object.assign(this.form, {
        id: this.id,
        company_business_license: this.company_business_license,
      });
      url = "Partner/Setup/company_authentication";
    }
    wx.showLoading({
      title: '提交数据...',
      mask: true,
    })
    app.ajax({
      url,
      data: this.form,
      success: res => {
        wx.hideLoading();
        if (res.statusCode == 200) {
          if (res.data.code == 1000) {
            app.showToast(res.data.message, "success");
            //执行刷新数据操作
            setTimeout(() => {
              const pages = getCurrentPages();
              if (pages.length >= 2) {
                const entrance = pages[pages.length - 2];
                if (~entrance.route.indexOf('entrance')) {
                  entrance.getAuthentic();
                }
              }
              wx.navigateBack({
                delta: 1
              })
            }, 2000)
          } else {
            app.showToast(res.data.message);
          }
        } else {
          app.showToast("网络错误!请稍后再试~");
        }
        console.log(res)
      },
      error: e => {
        wx.hideLoading();
        app.showToast("请求错误");
      }
    })

  },


  //预览图片
  previewImg(e) {
    const {
      current
    } = e.currentTarget.dataset;
    let urls = [];
    if (this.data.frontImg) {
      urls.push(this.data.frontImg);
    }
    if (this.data.backImg) {
      urls.push(this.data.backImg);
    }
    wx.previewImage({
      current,
      urls
    })
  },


})